package com.beep.erp.model.earlystage.log;


import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.BaseFragment;
import com.beep.erp.common.bean.EarlyStageListBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.recyclerview.adapter.HeaderAndFooterRecyclerViewAdapter;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.refresh.container.DefaultFooter;
import com.beep.erp.common.widget.refresh.container.DefaultHeader;
import com.beep.erp.model.purchase.PurchaseListAdapter;
import com.beep.erp.model.stock.StockAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class EarlyStageLogFragment extends BaseFragment<StockLogListPresenter, StockLogListModel> implements StockLogListContract.View, RefreshView.OnFreshListener {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.super_refresh)
    RefreshView superRefresh;
    @BindView(R.id.activity_my_follow)
    CoordinatorLayout activityMyFollow;

    private int mPageNum = 1;
    private StockLogListAdapter mStockLogListAdapter;
    private ArrayList<EarlyStageListBean.InfoBean> mListBean = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_early_stage;
    }

    @Override
    protected void initView(View view) {
        mTipsHelper = createTipsHelper(superRefresh);
        superRefresh.setListener(this);
        superRefresh.setHeader(new DefaultHeader(getActivity(), true));
        superRefresh.setFooter(new DefaultFooter(getActivity(), true));

        mStockLogListAdapter = new StockLogListAdapter(getActivity(), mListBean);
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(mStockLogListAdapter);
    }

    @Override
    protected void lazyLoad() {
        if ((!isPrepared || !isVisible)) {
            return;
        }
        isPrepared = false;
        mListBean.clear();
        mPageNum = 1;
        mTipsHelper.showLoading(true);
        new Handler().postDelayed(() -> {
            getStockLogData();
        }, 0);
    }

    private void getStockLogData() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("pageSize", Constants.PAGE_SIZE + "");
        map.put("pageNo", mPageNum + "");
        mPresenter.getStockLogList(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", Constants.PAGE_SIZE, mPageNum);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        superRefresh.onFinishFreshAndLoad();
        mTipsHelper.hideLoading();
        mTipsHelper.hideError();
        mTipsHelper.hideEmptyType();
    }

    @Override
    public void showMessage(String message) {
        if (ToolsUtils.isEmpty(message)) return;
        if (message.equals(getString(R.string.connection_timed_out)) || message.equals(getString(R.string.error)) || message.equals(getString(R.string.network_error)) || message.equals(getString(R.string.connection_failed)) || message.equals(getString(R.string.certificate_validation_failed))) {
            mTipsHelper.showError(true, message, v -> {
                mTipsHelper.hideError();
                onRefresh();
            });
        } else {
            ToastUtils.showShort(message);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void getDate() {

    }

    @Override
    public void getStockLogList(EarlyStageListBean earlyStageListBean) {
        if (ToolsUtils.isEmptyList(earlyStageListBean.getInfo()) && mPageNum > 1) {
            ToastUtils.showShort(getString(R.string.no_data));
            return;
        }
        mListBean.addAll(earlyStageListBean.getInfo());
        mStockLogListAdapter.notifyDataSetChanged();
        if (earlyStageListBean.getInfo().size() == 0) {
            mTipsHelper.showEmptyType(getString(R.string.emptymessage15));
        }
    }

    @Override
    public void onRefresh() {
        mListBean.clear();
        mPageNum = 1;
        mStockLogListAdapter.notifyDataSetChanged();
        getStockLogData();
    }

    @Override
    public void onLoadmore() {
        mPageNum++;
        getStockLogData();
    }
}
